﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using BLToolkit.Data.Linq;
using Nd.Tool;

namespace Paaboo.Todo.Library
{
    public class TaskBO
    {
        /// <summary>
        /// 添加任务
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="title"></param>
        /// <param name="catalogId"></param>
        /// <param name="dueDate"></param>
        /// <param name="priority"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public static bool Add(
            long userId,
            string title,
            int catalogId,
            DateTime dueDate,
            Priority priority = Priority.Normal,
            string description = "")
        {
            object result = TaskDB.GetTable<TaskEN>()
                .InsertWithIdentity(() => new TaskEN
            {
                ProjectId = catalogId,
                Description = description,
                DueDate = dueDate,
                Priority = priority,
                Status = Status.New,
                Title = title,
                UserId = userId,
            });
            return (result != null);
        }
        /// <summary>
        /// 获取用户未完成工作清单
        /// 最多取100条
        /// </summary>
        /// <param name="userId"></param>
        /// <returns>max length = 100</returns>
        public static List<TaskEN> GetUserNormalTaskList(long userId)
        {
            return TaskDB.GetTable<TaskEN>()
                .Where(o => o.UserId == userId && o.Status == Status.New)
                .Take(100)
                .OrderByDescending(o=>o.DueDate)
                .ToList();
        }
        /// <summary>
        /// 获取用户已完成任务列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PagingResult<TaskEN> GetUserCompletedTaskList(long userId, int pageIndex = 1, int pageSize = 10)
        {
            var countQuery = TaskDB.GetTable<TaskEN>()
                .Where(o => o.UserId == userId && o.Status == Status.Completed);
            var selectQuery = countQuery.OrderByDescending(o => o.DueDate);

            return PagingResult.Create(countQuery, selectQuery, pageIndex, pageSize, true);
        }
        /// <summary>
        /// 获取用户删除的任务列表
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public static PagingResult<TaskEN> GetUserDeletedTaskList(long userId, int pageIndex = 1, int pageSize = 10)
        {
            var countQuery = TaskDB.GetTable<TaskEN>()
                .Where(o => o.UserId == userId && o.Status == Status.Deleted);
            var selectQuery = countQuery.OrderByDescending(o => o.DueDate);

            return PagingResult.Create(countQuery, selectQuery, pageIndex, pageSize, true);
        }
        /// <summary>
        /// 快速设置任务到某个时间
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="taskId"></param>
        /// <param name="newDueDate"></param>
        /// <returns></returns>
        public static bool SetToSomeDay(long userId, int taskId, DateTime newDueDate)
        {
            int result = TaskDB.GetTable<TaskEN>()
                .Where(o => o.UserId == userId && o.TaskId == taskId)
                .Set(o => o.DueDate, newDueDate)
                .Update();
            return result > 0;
        }
        /// <summary>
        /// 详细编辑一个任务
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="taskId"></param>
        /// <param name="status"></param>
        /// <param name="title"></param>
        /// <param name="catalogId"></param>
        /// <param name="dueDate"></param>
        /// <param name="priority"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public static bool Edit(long userId,
            int taskId, 
            Status status,
            string title,
            int catalogId,
            DateTime dueDate,
            Priority priority = Priority.Normal,
            string description = "")
        {
            int result = TaskDB.GetTable<TaskEN>()
                .Where(o => o.UserId == userId && o.TaskId == taskId)
                .Set(o => o.Title, title)
                .Set(o => o.Status, status)
                .Set(o => o.Priority, priority)
                .Set(o => o.ProjectId, catalogId)
                .Set(o => o.Description, description)
                .Set(o => o.DueDate, dueDate)
                .Update();
            return result > 0;
        }
        /// <summary>
        /// 删除一个任务
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="taskId"></param>
        /// <returns></returns>
        public static bool Delete(long userId,int taskId)
        {
            TaskDB.GetTable<TaskEN>()
               .Where(o => o.TaskId == taskId && o.UserId == userId)
               .Set(o => o.Status, Status.Deleted)
               .Update();
            return true;
        }
        public static bool Completed(long userId, int taskId)
        {
            TaskDB.GetTable<TaskEN>()
               .Where(o => o.TaskId == taskId && o.UserId == userId)
               .Set(o => o.Status, Status.Completed)
               .Update();
            return true;
        }
    }
}
